package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheService;
import com.qf.model.StandardSubmit;
import com.qf.service.StrategyService;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 号段补全策略
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/22 21:37
 */
@Component(value = "phase")
@Slf4j
public class PhaseFilterServiceImpl implements StrategyService {

    @Autowired
    private CacheService cacheService;
    @Override
    public Boolean execute(StandardSubmit standardSubmit) {
        log.info("【策略模块 - 号段补全策略】开始执行，短信对象内容 standardSubmit={}",standardSubmit);

        //1.获取短信发送的目标手机号
        String destMobile = standardSubmit.getDestMobile();

        //2.根据手机号判断手机号的运营商
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            //移动的运营商
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);

        }else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            //联通的运营商
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //电信的运营商
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
        }else {
            //未知运营商
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }

        //3.获取目标手机号的前七位
        String subMobile = destMobile.substring(0, 7);

        //4.根据手机号前七位判断手机号的归属地，省，市
        String provinceAndCity = cacheService.get(CacheConstants.CACHE_PREFIX_PHASE + subMobile);
        provinceAndCity = provinceAndCity.replace("\"","");
        if (provinceAndCity == null){
            log.info("【策略模块 - 号码补全策略】 目标号码没有归属地，destMobile={}",destMobile);
            return false;
        }
        String[] provinceAndCityArray = provinceAndCity.split(",");
        if (provinceAndCityArray.length < 2){
            log.info("【策略模块 - 号码补全策略】 目标号码没有归属地，destMobile={}",destMobile);
            return false;

        }
        standardSubmit.setProvinceId(Integer.parseInt(provinceAndCityArray[0]));
        standardSubmit.setCityId(Integer.parseInt(provinceAndCityArray[1]));

        //5.获取通道数据
        Map<String,String> channel = cacheService.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL
                + standardSubmit.getOperatorId()
                + ":" + standardSubmit.getGatewayID());

        //6.获取路由数据
        Map<String, String> router = cacheService.hMGet(CacheConstants.CACHE_PREFIX_ROUTER
                + standardSubmit.getClientID()
                + ":" + standardSubmit.getGatewayID());

        //7.补全短信对象中的下发源号码 spnumber + extendnumber
        String spnumber = channel.get("spnumber");
        String extendnumber = router.get("extendnumber");
        standardSubmit.setSrcNumber(spnumber + extendnumber);

        //8.根据运营商的不同补全这条短信的费用
        String price = router.get("price");
        standardSubmit.setFee(Long.valueOf(price));
        log.info("【策略模块 - 号码补全策略】成功执行完毕！ standardSubmit={}",standardSubmit);
        return true;
    }
}
